import axios from "axios";

const DUMMYJSON_BASE_URL = "https://dummyjson.com";
const BASE_URL = "http://localhost:5000";

export const getPosts = async () => {
  const { data } = await axios.get(`${DUMMYJSON_BASE_URL}/posts?limit=10`);
  return data.posts;
};

export const getPeople = async () => {
  const { data } = await axios.get(`${DUMMYJSON_BASE_URL}/users?limit=10`);
  return data.users;
};

export const signin = async (username, password) => {
  const { data } = await axios.post(
    `${BASE_URL}/login`,
    {
      username,
      password,
    },
    {
      withCredentials: true,
    }
  );
  return data;
};

export const logoutUser = async () => {
  return await axios.post(
    `${BASE_URL}/logout`,
    {},
    {
      withCredentials: true,
    }
  );
};

export const startVerify = async (userID) => {
  const { data } = await axios.post(
    `${BASE_URL}/startVerify`,
    {
      userID,
    },
    {
      withCredentials: true,
    }
  );
  return data;
};

export const verify = async (userID, assertion) => {
  return await axios.post(
    `${BASE_URL}/verify`,
    {
      userID,
      assertion,
    },
    {
      withCredentials: true,
    }
  );
};

export const registerTokenAuth = async (endpoint) => {
  const { data } = await axios.post(
    `${BASE_URL}${endpoint}`,
    {},
    {
      withCredentials: true,
    }
  );
  return data;
};

export const register = async (attestation) => {
  const { data } = await axios.post(
    `${BASE_URL}/register`,
    {
      attestation,
    },
    {
      withCredentials: true,
    }
  );
  return data;
};
